﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using SMS.Entities.BaseClasses;
using System.ComponentModel;
using System.Collections.Generic;
using System.Windows.Data;

namespace SMS.ViewModel.Admin
{
    public class ViewModel_ResetPassword :BaseNotify
    {
        #region "Page Constructor"
        SMS.Model.AuthenticationModel model;
        
        public ViewModel_ResetPassword()
        {
            if (!DesignerProperties.IsInDesignTool)
            {
                model = new Model.AuthenticationModel();
                //model.OnServiceError += new Action<Entities.SMSException>(_model_OnServiceError);

                model.AdminResetPasswordCompleted += new Action<int>(model_AdminResetPasswordCompleted);   
                model.GetApplicationUserListCompleted += new Action<System.Collections.ObjectModel.ObservableCollection<Entities.Admin.ApplicationUser>>(model_GetApplicationUserListCompleted);
            }
            SearchCommand = new SMS.Common.Silverlight.DelegateCommand(Search_Click, null);
            ResetCommand = new SMS.Common.Silverlight.DelegateCommand(Reset, null);
            
        }


        void model_GetApplicationUserListCompleted(System.Collections.ObjectModel.ObservableCollection<Entities.Admin.ApplicationUser> obj)
        {
            UserMessage = obj.Count + " Users found";
            UsersList = new List<Entities.Admin.ApplicationUser>(obj);
            PagedUsersList = new PagedCollectionView(UsersList);
        }

       #endregion

        #region "Property"

        private List<Entities.Admin.ApplicationUser> _UsersList;

        public List<Entities.Admin.ApplicationUser> UsersList
        {
            get { return _UsersList; }
            set { _UsersList = value; OnPropertyChanged("UsersList"); }
        }



        private PagedCollectionView _PagedUsersList;

        public PagedCollectionView PagedUsersList
        {
            get { return _PagedUsersList; }
            set { _PagedUsersList = value; OnPropertyChanged("PagedUsersList"); }
        }


        private string  _userName;

        public string  UserName
        {
            get { return _userName; }
            set { _userName = value; OnPropertyChanged("UserName"); }
        }
        private string _password;

        public string Password
        {
            get { return _password; }
            set { _password = value; OnPropertyChanged("Password"); }
        }
        private string _confirmPassword;

        public string ConfirmPassword
        {
            get { return _confirmPassword; }
            set { _confirmPassword = value; OnPropertyChanged("ConfirmPassword"); }
        }

        private Entities.Admin.ApplicationUser _SelectedMaster;

        public Entities.Admin.ApplicationUser SelectedMaster
        {
            get { return _SelectedMaster; }
            set
            {
                _SelectedMaster = value;
                if (value != null)
                    UserName = SelectedMaster.UserName;
                OnPropertyChanged("SelectedMaster");
            }
        }

        private string  _SearchText;

        public string  SearchText
        {
            get { return _SearchText; }
            set { _SearchText = value; OnPropertyChanged("SearchText"); }
        }
        
        
        #endregion

        #region "Methods"

        public void Reset(object param)
        {
            if (Password != ConfirmPassword)
            {
                MessageBox.Show("Password must be match");
                return;
            }
            if (SelectedMaster != null)
            {
                model.AdminResetPassword(UserName, Password);
                
                UserMessage = "Saving password...";
            }
            else
            {
                ErrorMessage = "No Password to save";
            }
            
        }

        void model_AdminResetPasswordCompleted(int obj)
        {
            UserMessage = "Reset Password successfully.";
            SelectedMaster.Login_ID = obj;
        }

        public void Search_Click(object param)
        {
            model.GetApplicationUserList(App.LoggedinUserName, SearchText);

            UserMessage = "Searching customer...";
        }
        #endregion

        #region "Validation Property"

        #endregion

        #region "Delegate Command"

        public ICommand ResetCommand { get; set; }

        public ICommand SearchCommand { get; private set; }
        #endregion


    }
}
